package cn.yh.st.core.dataSource;

import org.aspectj.lang.JoinPoint;

public class DataSourceAspect {
	public void before(JoinPoint point) {

		// 获取到当前执行的方法名

		String methodName = point.getSignature().getName();

		if (isSlave(methodName)) {

			// 标记为读库
			HandleDataSource.markSlave();

		} else {
			// 标记为写库
			HandleDataSource.markMaster();

		}

	}

	/**
	 * 
	 * 判断是否为读库
	 * 
	 * 
	 * 
	 * @param methodName
	 * 
	 * @return
	 */

	private Boolean isSlave(String methodName) {

		// 方法名以query、find、get开头的方法名走从库

		return startWithdAny(methodName, "query", "find", "get");

	}

	public static boolean startWithdAny(String method, String... condition) {
		for (String coString : condition) {
			if (method.startsWith(coString))
				return true;
		}
		return false;
	}
}
